﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using yt = Google.YouTube;

public partial class ytai : System.Web.UI.MasterPage
{
	public readonly string GoogleApiKey = ConfigurationManager.AppSettings["GoogleApiKey"];
	public string Token;

    protected void Page_Init(object sender, EventArgs e)
    {
		// make sure they're on www
		if (Request.Url.AbsoluteUri.Contains("cowhut.com") && !Request.Url.AbsoluteUri.Contains("www.cowhut.com"))
			Response.Redirect(Request.Url.AbsoluteUri.Replace("cowhut.com", "www.cowhut.com"));

		Uri targetUri = new Uri(Request.Url, "home.aspx");

		GotoAuthSubLink.Visible = false;

		if (Session["token"] != null && !Request.Url.AbsoluteUri.ToLower().Contains(targetUri.AbsoluteUri.ToLower()))
		{
			Response.Redirect(targetUri.ToString());
		}
		else if (Session["token"] == null && Request.QueryString["token"] != null)
		{
			String token = Request.QueryString["token"];
			//Session["token"] = AuthSubUtil.exchangeForSessionToken("http", "www.cowhut.com", token, null);
			Token = AuthSubUtil.exchangeForSessionToken(token, null);
			Session["token"] = Token;

			//Response.Redirect(targetUri.ToString(), true);
			//Server.Transfer("home.aspx", false);
		}
		else if (Session["token"] == null) //no auth data, print link
		{
			GotoAuthSubLink.Text = "&nbsp;Login to your Google Account&nbsp;";
			GotoAuthSubLink.Visible = true;
			GotoAuthSubLink.NavigateUrl = AuthSubUtil.getRequestUrl("http://www.cowhut.com/home.aspx", "http://gdata.youtube.com", false, true);
		}

    }
}
